<?php

class AccountController extends Controller
{

    public function actionStatus() {
        $mac = Yii::app()->request->getParam('mac');
        $accName = Yii::app()->user->getState("accName");//Yii::app()->request->getParam('accName');

        $accountDao = new AccountDAO();
        $statusList = $accountDao->findStatus($accName, $mac);
        $newSite = array();

        if ($statusList) {
            foreach ($statusList as $index => $site) {
                $newSite[$site['siteId']] = $site;
            }
        }

        if (count($newSite) > 0) {
            $this->_sendAjaxResponse(json_encode(array('status' => Constants::RESPONSE_SUCCESS, 'data' => $newSite)));
        } else {
            $this->_sendAjaxResponse(json_encode(array('status' => Constants::RESPONSE_ERROR, 'data' => $statusList)));
        }

    }

    public function actionUpdate() {
        $mac = Yii::app()->request->getParam('mac');
        $accName = Yii::app()->user->getState("accName");
        $siteInfo = Yii::app()->request->getParam('siteInfo');
        $capStatus = Yii::app()->request->getParam('capStatus');
        $siteInfo = json_decode($siteInfo);
        $capStatus = json_decode($capStatus);

        $accountTrackingDao = new AccountTrackingDAO();
        $capchaDAO = new CapchaDAO();

        if ($siteInfo) {
            $acountList = Yii::app()->user->getState('accountList');
            foreach($siteInfo as $siteId => $site) {
                $accountTrackingDao->insert($mac, $site);
            }
        }

        if ($capStatus) {
            foreach($capStatus as $index => $capcha) {
                $capchaDAO->updateStatus($capcha->capchaId, $capcha->status);
            }
        }

        $this->_sendAjaxResponse(json_encode(array('status' => Constants::RESPONSE_SUCCESS)));
    }
}